<template>
	<view class="bidding-sort">
		<jhj-sort ref="sort" class="sort" :sortData="sortData" @click="sortClick"></jhj-sort>
		<view class="content">
			<you-scroll ref="scroll" :fetch="getBiddingData">
				<view slot-scope="{row,index}" @click="goDetailPage(row,index)">
					<bidding-list-item :itemData="row"></bidding-list-item>
				</view>
			</you-scroll>
		</view>
	</view>
</template>

<script>
	import BiddingListItem from './components/BiddingListItem.vue'
	import youScroll from '@/components/uni-list-scroll/you-scroll'
	import JhjSort from '@/components/jhj-sort/jhj-sort.vue'
	import {
		getBiddingList,
		getBiddingList2
	} from '@/static/js/api/bidding.js';
	var self;
	export default {
		components: {
			JhjSort,
			youScroll,
			BiddingListItem
		},
		data() {
			return {
				sortData: [
					[{
						name: '全部',
						isCheck: true
					}, {
						name: '今日',
						isCheck: false
					}, {
						name: '近三天',
						isCheck: false
					}, {
						name: '近七天',
						isCheck: false
					}, {
						name: '近两周',
						isCheck: false
					}],
					[{
						name: '全部',
						isCheck: true
					}, {
						name: '招标公告',
						isCheck: false
					}, {
						name: '中标公告',
						isCheck: false
					}, {
						name: '更正公告',
						isCheck: false
					}],
					[{
						name: '全部',
						isCheck: true
					}, {
						name: '企业',
						isCheck: false
					}, {
						name: '班组',
						isCheck: false
					}],
					[{
						name: '全部',
						isCheck: true
					}, {
						name: '公开招标',
						isCheck: false
					}, {
						name: '邀请招标',
						isCheck: false
					}],
					[{
						name: '全部',
						isCheck: true
					}, {
						name: '50以内',
						isCheck: false
					}, {
						name: '50-99',
						isCheck: false
					}, {
						name: '100-199',
						isCheck: false
					}, {
						name: '200-499',
						isCheck: false
					}, {
						name: '500以上',
						isCheck: false
					}]
				],
				dateList: [],
				regist_qualification_set: '',
				bidding_type: '',
				people_number: [],
				type: ''
			}
		},
		onLoad(option) {
			this.regist_qualification_set = option.regist=='全部分类'?'':option.regist;
		},
		mounted() {
			self = this;
			let i1 = 2;
			let i2 = 0;
			self.sortData[2].forEach((item2, index2) => {
				if (self.regist_qualification_set == item2.name) {
					i2 = index2
				} else {
					item2.isCheck = false;
				}
			})

			this.$refs.sort.changeData(2, i2)
			this.$refs.scroll.loadData();
		},
		methods: {
			getBiddingData(pageIndex, pageSize) {
				// let domain = '[]';
				// if (this.state) {
				// 	domain = `[('state','=','${this.state}'),]`
				// }
				let domain = '[';
				// if (this.state) {
				// 	domain += `('state','=','${this.state}'),`
				// }
				if (self.dateList.length > 0) {
					domain += `('create_date','>=','${self.dateList[0]}'),('create_date','<=','${self.dateList[1]}'),`
				}

				if (self.regist_qualification_set) {
					domain += `('regist_qualification_set','=','${self.regist_qualification_set}'),`
				}
				if (self.bidding_type) {
					domain += `('bidding_type','=','${self.bidding_type}'),`
				}
				if (self.people_number.length > 0) {
					domain += `('people_number','>=','${self.people_number[0]}'),('people_number','<','${self.people_number[1]}'),`
				}
				if (self.type) {
					domain += `('type','=','${self.type}'),`
				}
				domain += ']'
				
				if(uni.getStorageSync('userInfo')){
					return getBiddingList(pageIndex,pageSize,domain).then(res=>{
						return res.data;
					})
				}else{
					return getBiddingList2(pageIndex,pageSize,domain).then(res=>{
						return res.data;
					})
				}
				// return getBiddingList(pageIndex, pageSize, domain).then(res => {
				// 	return res.data;
				// })
			},
			sortClick(item, index, index2) {

				if (index == 0) {

					if (index2 == 1) {
						this.dateList = [this.getDay(0), this.getDay(0)]
					} else if (index2 == 2) {
						this.dateList = [this.getDay(-3), this.getDay(0)]
					} else if (index2 == 3) {
						this.dateList = [this.getDay(-7), this.getDay(0)]
					} else if (index2 == 4) {
						this.dateList = [this.getDay(-14), this.getDay(0)]
					} else {
						this.dateList = [];
					}
				} else if (index == 1) {
					if (item.name == '全部') {
						this.type = '';
					} else if (item.name == '招标公告') {
						this.type = '招标'
					} else if (item.name == '中标公告') {
						this.type = '中标'
					} else if (item.name == '更正公告') {
						this.type = '变更公告'
					}

				} else if (index == 2) {
					if (item.name != '全部') {
						this.regist_qualification_set = item.name;
					} else {
						this.regist_qualification_set = '';
					}


				} else if (index == 3) {
					if (item.name != '全部') {
						this.bidding_type = item.name;
					} else {
						this.bidding_type = '';
					}

				} else if (index == 4) {

					if (index2 == 1) {
						this.people_number = [0, 50]
					} else if (index2 == 2) {
						this.people_number = [50, 99]
					} else if (index2 == 3) {
						this.people_number = [100, 199]
					} else if (index2 == 4) {
						this.people_number = [200, 499]
					} else if (index2 == 5) {
						this.people_number = [500, 10000000]
					} else {
						this.people_number = [];
					}
				}
				this.$refs.scroll.loadData();

				// if(index)
			},
			getDay(day) {
				var today = new Date();
				var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
				today.setTime(targetday_milliseconds); //注意，这行是关键代码
				var tYear = today.getFullYear();
				var tMonth = today.getMonth();
				var tDate = today.getDate();
				tMonth = this.doHandleMonth(tMonth + 1);
				tDate = this.doHandleMonth(tDate);
				return tYear + "-" + tMonth + "-" + tDate;
			},
			doHandleMonth(month) {
				var m = month;
				if (month.toString().length == 1) {
					m = "0" + month;
				}
				return m;
			},
			goDetailPage(item,index){
				
				uni.navigateTo({
					url: `/pages/home/bidding/BiddingDetailPage?id=${item.id}&type=${item.type}`
				})
			}
		}
	}
</script>

<style lang="less" scoped>
	.bidding-sort {
		height: 100vh;
		position: relative;
		overflow: hidden;
	}

	.sort {
		position: absolute;
		top: 0;
		right: 0;
		left: 0;
	}

	.content {
		height: calc(100% - 600rpx);
		margin-top: 600rpx;
	}
</style>
